//
//  MKBaseRequest.swift
//  MOKOAnnounce
//
//  Created by _SS on 2017/7/10.
//  Copyright © 2017年 cc.moko. All rights reserved.
//

import Foundation
import Alamofire

//开发库
//let MOKO_VERSION_PATH = "http://version.moko.io/"
//let MOKO_OPENFIRE_HOST = "mbtest.moko.io"
//let MOKO_OPENFIRE_PORT = 5222
//let MOKO_APP_KEY = "iosappid"
//let MOKO_APP_SECRET = "ddeerrff"
//let UMANALYTICS_CHANNEL = "TestChannel2"
//let MOKO_CHANNEL_TYPE = "30"
//let UMSHAREDAPPKEY = "549a6970fd98c55cad000f81"
//let MK_HXAPPKEY = "mokocc#mkmbintfdev"
//let JPushAppKey = "e6b860c97599f0ed6d6aca44"
//let JPushChannel = "iOSChannel"
//let JPushIsProduction = false

//正式库
let MOKO_VERSION_PATH = "https://versions.moko.cc/"
let MOKO_OPENFIRE_HOST = "chat.moko.cc"
let MOKO_OPENFIRE_PORT = 5222
let MOKO_APP_KEY = "J6ZrenTkbScVxxF"
let MOKO_APP_SECRET = "JRqtDuJ7e4hA1b7XKd5x"
let UMANALYTICS_CHANNEL = "MokoChannel"
let MOKO_CHANNEL_TYPE = "3"
let UMSHAREDAPPKEY = "549a6970fd98c55cad000f81"
let MK_HXAPPKEY = "mokocc#mkmbintf"
let JPushAppKey = "e6b860c97599f0ed6d6aca44"
let JPushChannel = "iOSChannel"
let JPushIsProduction = true





protocol Request {
    // 在protocal中定义属性，指明属性支持的操作 只读{ get } 或者可读写 { get set }
    var path: String { get }
    var method: HTTPMethod { get }
    var parameter: [String: AnyObject] { get }
    // associatedtype 声明一个类型的占位符作为协议中的一部分
    associatedtype Analysis: Decodable
}

protocol HeaderRequest {
    var path: String { get }
    var method: HTTPMethod { get }
    var header: HTTPHeaders { get }
    var parameter: [String: AnyObject] { get }
    associatedtype Analysis: Decodable
}

protocol Decodable {
    static func parse(anyData: Any) ->Self?
}

protocol SenderRequest {
    func send<T: Request>(_ p: T, analysis: @escaping (T.Analysis?) -> Void)
}

protocol SenderHeaderRequest {
    func send<T: HeaderRequest>(_ p: T, analysis: @escaping (T.Analysis?) -> Void)
}

struct SessionRequestSender:SenderRequest {
    func send<T : Request>(_ p: T, analysis: @escaping (T.Analysis?) -> Void) {
        // 10 秒超时
        Alamofire.SessionManager.default.session.configuration.timeoutIntervalForRequest = 10
        Alamofire.request(p.path, method: p.method, parameters: p.parameter).responseJSON { (response) in
            switch response.result {
            case .success:
                if let anyData = response.result.value, let res = T.Analysis.parse(anyData: anyData) {
                    DispatchQueue.main.async {
                        analysis(res)
                    }
                }
            case .failure(_):
                break
            }
        }
    }
}

struct SessionRequestPostSender:SenderHeaderRequest {
    
    func send<T : HeaderRequest>(_ p: T, analysis: @escaping (T.Analysis?) -> Void) {
        // 10 秒超时
        Alamofire.SessionManager.default.session.configuration.timeoutIntervalForRequest = 10
        Alamofire.request(p.path, method: p.method, parameters: p.parameter, encoding: JSONEncoding.default, headers: p.header).responseJSON { (response) in
            switch response.result {
            case .success:
                if let anyData = response.result.value, let res = T.Analysis.parse(anyData: anyData) {
                    DispatchQueue.main.async {
                        analysis(res)
                    }
                }
            case .failure(_):
                break
            }
        }
    }
}
